<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head lang="en" th:replace="include/common/common :: commonHeader">
    <footer th:include="include/common/common :: layUiResource"></footer>
    <footer th:include="include/common/common :: blueCSSResource"></footer>
    <footer th:include="include/common/common :: zTreeCSSSource"></footer>
    <footer th:include="include/common/common :: zTreeJSSource"></footer>
    <script>
        layui.config({
            base: '/staticResource/layuiadmin/' //静态资源所在路径
        }).extend({
            index: 'lib/index' //主入口模块
        }).use(['index','form'] , function () {
            var admin = layui.admin
                ,element = layui.element
                ,table = layui.table
                ,form = layui.form;

            element.render();

            $(document).on('click','.zTreeBtn' , function () {
                $("#zTreeOption").val($(this).attr('data'));
                let elementId = $(this).attr('id') , data = JSON.parse($(this).attr('data')) , i = '/a/layTips/treeSelectForm?isUpdateTree=false' , t = data.title;
                $.fn.blueJs.tools.active['layerOpen'].call(this , i , t);
            });

            let entityName = 'goods'
                ,tableId = entityName + "-table-operate"
                ,_id = $('#goodsId').val()
                ,_productCode = $('#goodsCode').val()
                ,isSafe = false
                ,cols = [
                [
                    {field:'id', title: 'ID', sort: true, hide:true, fixed: 'left'}
                    ,{field:'code', sort:true , title: '商品代码' , hide:true, edit:true}
                    ,{field:'barcode', sort:true , title: '条码' , edit:true}
                    ,{field:'spec', sort:true , title: '单位' , edit:true}
                    ,{field:'unit', sort:true , title: '包装单位' , edit:true}
                    ,{field:'qpcStr', sort:true , title: '包装规格' , edit:false}
                    ,{field:'qpc', sort:true , title: '包装数量' , edit:true}
                    ,{field:'oprice', title: '单价' , edit:true}
                ]
            ];
            let templateData = $.fn.blueJs.tools.active['mapCloneKey'].call(this , cols[0]);

            $(document).ready(function () {
                $.fn.blueJs.tools.formButtonItem(entityName , 'goods:edit');
                let searchBtn = '#' + entityName + 'SearchBtn' ;
                $("#searchId").val(_id);
                $("#searchCode").val(_productCode);
                let tableData = $.fn.blueJs.tools.active['getPageData'].call(this , searchBtn);
                $.fn.blueJs.tools.active['tableWriteProtect'].call(this , table , tableId , tableData , cols , isSafe);
                if ($.fn.blueJs.tools.isEmpty(table.cache[tableId])){
                    $.fn.blueJs.tools.active['tableAddRow'].call(this , table , tableId , templateData , 1 , true , 'barcode');
                }

            });

            //点击事件监听
            $(document).on('click' , function (event) {
                let targetId = event.target.id;
                let cancelBtn = entityName + "CancelBtn";
                if (targetId == cancelBtn){
                    let index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                    parent.layer.close(index); //再执行关闭
                }
            });

            $('.goods-table-operate-btn .layui-btn').on('click', function(){
                var type = $(this).data('type');
                if (type == 'add'){
                    $.fn.blueJs.tools.active['tableAddRow'].call(this , table , tableId , templateData , 1 , true , 'barcode');
                }
            });

            $(document).on('keyup' , '.layui-input' , function (event) {
                switch (event.keyCode) {
                    case 13:
                        var td = $(this).parent('td') , tr = td.parent('tr') , dataIndex = parseInt($(tr).attr("data-index")) , data_field = $(td).attr("data-field")
                            , tableDataList = table.cache[tableId];
                        switch (data_field) {
                            case 'oprice':
                                $.fn.blueJs.tools.active['tableAddRow'].call(this , table , tableId , templateData , 1 , true , 'barcode');
                                break;
                            case 'qpc':
                                let _qpc = table.cache[tableId][dataIndex]['qpc'];
                                let regStr = /^[1-9][0-9]*$/;
                                console.log(regStr.test(_qpc));
                                if (regStr.test(_qpc)){
                                    let _qpcStr = "1*" + _qpc;
                                    console.log(_qpcStr);
                                    $.fn.blueJs.tools.active['tableColUpdate'].call(this , table , tableId , dataIndex , 'qpcStr' , _qpcStr);
                                }else {
                                    layer.msg("包装数量必须为整数类型，请重新输入！");
                                };
                                $.fn.blueJs.tools.active['tableReload'].call(this , table , tableId);
                                $.fn.blueJs.tools.active['tableColClick'].call(this , tableId , dataIndex , 'oprice');
                                break;
                            default:
                                td['nextAll']('[data-edit="true"]:first').click();
                        }
                        break;
                }
            });

            form.on('submit(goodsSave)',function () {
                let formId = $(this).attr("blue-form-id"),
                    vProductCode = $('#goodsCode').val();
                if ($.fn.blueJs.tools.isEmpty(vProductCode)){
                    layer.msg("商品代码不允许为空！");
                }else {
                    let tableDataList = table.cache[tableId] , submitDtlList = {};

                    $.each(tableDataList , function (i , v) {
                        let vBarcode = v.barcode;
                        if ($.fn.blueJs.tools.isNotEmpty(vBarcode)){
                            submitDtlList["productList["+i+"].code"] = vProductCode;
                            submitDtlList["productList["+i+"].barcode"] = vBarcode;
                            submitDtlList["productList["+i+"].spec"] = v.spec;
                            submitDtlList["productList["+i+"].unit"] = v.unit;
                            submitDtlList["productList["+i+"].qpcStr"] = v.qpcStr;
                            submitDtlList["productList["+i+"].qpc"] = v.qpc;
                            submitDtlList["productList["+i+"].oprice"] = v.oprice;
                        }
                    });

                    let _id = $("#" + entityName + "Id").val() , _isNewRecord = $("#isNewRecord").val() , vGoodsName = $('#goodsName').val();
                    let vClsCode = $('#clsItemCode').val() , vBrandCode = $('#brandItemCode').val();
                    let jsonData = "?id=" + _id + "&isNewRecord=" + _isNewRecord + "&code=" + vProductCode + "&name=" + vGoodsName + "&clsCode=" + vClsCode
                        ,formUrl = $("#" + formId).attr("action") , msg = "";
                    if ($.fn.blueJs.tools.isNotEmpty(vBrandCode)){
                        jsonData = jsonData + "&brandCode=" + vBrandCode;
                    }
                    $.ajax({
                        type:'POST',
                        url:formUrl + jsonData,
                        data:submitDtlList,
                        async:false,
                        dataType:'JSON',
                        success:function (data) {
                            let status = data.status;
                            if (status != 0){
                                layer.msg(data.msg + "请联系系统管理员！");
                            }else {
                                let type = 'saveMsg';
                                //当你在iframe页面关闭自身时
                                let index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                $.fn.blueJs.tools.active[type].call(this , data.msg  , index);
                            }
                        },
                        error:function () {

                        }
                    });
                }
/*                let msg = $.fn.blueJs.tools.formSubmit(this)
                    ,type = 'saveMsg';
                //当你在iframe页面关闭自身时
                let index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                $.fn.blueJs.tools.active[type].call(this , msg , index);*/
                return false;
            });

        });


    </script>
</head>
<body>
<!--  zTree遮罩层 -->
<input type="hidden" id="zTreeOption" value=""/>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card" id="form-head">
                <div class="layui-card-body">
                    <form class="layui-form" id="goodsForm" th:object="${goods}" method="post" action="/a/goods/save">
                        <!--<div class="box-body">-->
                        <div class="blue-form-unit">基本信息</div>
                        <input type="hidden" name="id" id="goodsId" th:field="*{id}"/>
                        <input type="hidden" name="isNewRecord" th:field="*{isNewRecord}"/>

                        <div class="layui-form-item">
                            <div class="layui-col-md6">
                                <label class="layui-form-label " title=""><span class="blue-required ">*</span>品类：</label>
                                <thl:tml type="treeSelect" excludeCode="clsCode" isShowCode="1" title = "品类信息" submitAttributeName = "code"
                                         url = "/a/clsItem/treeData" name = "clsCode" id = "clsItem" submitName = "clsCode" submitValue = "*{clsItem.treeCode}" showValue = "*{clsItem.treeName}"
                                         canSelectRoot = "false" canSelectParent = "false"/>
                            </div>

                            <div class="layui-col-md6">
                                <label class="layui-form-label " title=""><span class="blue-required hide">*</span>品牌：</label>
                                <thl:tml type="treeSelect" excludeCode="brandCode" isShowCode="1" title = "品牌信息" submitAttributeName = "code"
                                         url = "/a/brandItem/treeData" name = "brandCode" id = "brandItem" submitName = "code" submitValue = "*{brandItem.code}" showValue = "*{brandItem.name}"
                                         canSelectRoot = "true" canSelectParent = "true"/>
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <div class="layui-col-md6">
                                <label class="layui-form-label " title=""><span class="blue-required ">*</span>商品代码：</label>
                                <div class="layui-input-block">
                                    <input type="text" class="layui-input" lay-verify="required" placeholder="请输入代码" id="goodsCode" th:field="*{code}">
                                </div>
                            </div>

                            <div class="layui-col-md6">
                                <label class="layui-form-label " title=""><span class="blue-required ">*</span>商品名称：</label>
                                <div class="layui-input-block">
                                    <input type="text" class="layui-input" lay-verify="required" placeholder="请输入名称" id="goodsName" th:field="*{name}">
                                </div>
                            </div>
                        </div>

<!--                        <div class="layui-form-item">
                            <div class="layui-col-md6">
                                <label class="layui-form-label " title=""><span class="blue-required ">*</span>条码：</label>
                                <div class="layui-input-block">
                                    <input type="text" class="layui-input" lay-verify="required" placeholder="请输入条码" id="barcode" th:field="*{barcode}">
                                </div>
                            </div>

                            <div class="layui-col-md6">
                                <label class="layui-form-label " title=""><span class="blue-required ">*</span>进价：</label>
                                <div class="layui-input-block">
                                    <input type="text" class="layui-input" lay-verify="required" placeholder="" id="oprice" th:field="*{oprice}">
                                </div>
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <div class="layui-col-md6">
                                <label class="layui-form-label " title=""><span class="blue-required ">*</span>单位：</label>
                                <div class="layui-input-block">
                                    <input type="text" class="layui-input" lay-verify="" placeholder="此处为商品最小包装单位，例如：本、个、支等" id="spec" th:field="*{spec}" >
                                </div>
                            </div>

                            <div class="layui-col-md6">
                                <label class="layui-form-label " title=""><span class="blue-required ">*</span>包装单位：</label>
                                <div class="layui-input-block">
                                    <input type="text" class="layui-input" lay-verify="" placeholder="此处为商品包装单位，例如：包、箱等" id="unit" th:field="*{unit}">
                                </div>
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <div class="layui-col-md6">
                                <label class="layui-form-label " title=""><span class="blue-required ">*</span>包装系数：</label>
                                <div class="layui-input-block">
                                    <input type="text" class="layui-input" lay-verify="" placeholder="请输入正整数" id="qpc" th:field="*{qpc}">
                                </div>
                            </div>

                            <div class="layui-col-md6">
                                <label class="layui-form-label " title=""><span class="blue-required ">*</span>包装规格：</label>
                                <div class="layui-input-block">
                                    <input type="text" class="layui-input" lay-verify="" placeholder="" readonly id="qpcStr" th:field="*{qpcStr}">
                                </div>
                            </div>
                        </div>-->

                        <div class="layui-form-item">
                            <div class="layui-col-md12">
                                <label class="layui-form-label " title=""><span class="blue-required hide">*</span>备注：</label>
                                <div class="layui-input-block">
                                    <input type="text" id="remarks" class="layui-input" th:field="*{remarks}">
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
            </div>

            <div class="layui-card" id="form-dtl">
                <!--                        todo:id = entity + search + form-->
                <form class="layui-form" id="goodsSearchForm" th:object="${goods}" method="post" action="/a/goods/findPage4GdQpc">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <div class="layui-input-inline">
                                <input type="hidden" th:field="*{id}" id="searchId">
                                <input type="hidden" id='searchCode' th:field="*{code}"/>
                            </div>
                        </div>
                    </div>

                    <div class="layui-col-lg4">
                        <!--                        todo:blue-form-id = entity + search + form-->
                        <button type="button" style="display: none" class="layui-btn" id="goodsSearchBtn" onsubmit="return false" blue-form-id="goodsSearchForm">搜索</button>
                    </div>
                </form>

                <div class="layui-card-body">
                    <div class="layui-btn-group goods-table-operate-btn" id="tableBtn" style="margin-bottom: 10px;">
                        <button class="layui-btn layui-btn-sm" data-type="add">新增</button>
                    </div>
                    <table class="layui-hide" id="goods-table-operate" lay-filter="stkIn-table-operate"></table>

<!--                    <script type="text/html" id="stkIn-table-operate-barDemo">
                        <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">选择商品</a>
                    </script>-->
                </div>
            </div>

            <!-- Form 按钮组-->
            <div class="layui-card" id="form-submit-buttons">
                <div class="layui-card-body">
                    <div class="layui-form-item layui-layout-admin">
                        <div class="layui-input-block blue-form-button-item">
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>




